/*
 * Projectile - shooty thing that follows an arc and does damage
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.Lib;
import flash3f.Images;
import flash.display.BitmapData;
import flash.geom.Point;
#else neko
import lib3f.Point;
#end
import lib3f.IntPoint;

class Projectile
{
	#if flash9
	public var img : IsoBitmap;
	public var simg : IsoBitmap;
	#end
	public var destx : Float;
	public var desty : Float;
	public var desttx : Int;
	public var destty : Int;
	public var persistentoffy : Float;
	public var speed : Int;
	public var x : Float;
	public var y : Float;
	public var z : Float;
	public var vecx : Float;
	public var vecy : Float;
	public var startz : Float;
	public var startdist : Float;
	public var alive : Bool;
	public var parent : Shooter;
	
	public var shotup : Dynamic;
	public var hit : Dynamic;
	
	public function new(originx:Int, originy:Int, ndestx:Int, 
						ndesty:Int, height:Float, style:Int,
						nspeed:Int, parent:Shooter)
	{
		this.parent = parent;
		var startpoint : Point = IsoMetrics.tiletopix_xy(originx, originy);
		var destpoint : Point = IsoMetrics.tiletopix_xy(ndestx, ndesty);
		x = startpoint.x+11;
		y = startpoint.y+11;
		desttx = ndestx;
		destty = ndesty;
		destx = destpoint.x+11;
		desty = destpoint.y+11;
		z = height;
		startz = height;
		vecx = (x-destx);
		vecy = (y-desty);
		startdist = Math.sqrt((vecx*vecx)+(vecy*vecy));
		speed = nspeed;
		alive = true;		

		// for the turret ani's
		parent.angle = Math.atan2(vecx/startdist, vecy/startdist);
		x -= speed*(vecx/startdist);
		y -= speed*(vecy/startdist);
		
		#if flash9
			img = new IsoBitmap(Images.getdata(0x012345));
			simg = new IsoBitmap(Images.getdata(0x543210));
			img.px = x;			
			simg.px = x;			
			persistentoffy = - (img.bitmapData.height)/2;
			img.py = y;
			simg.py = y;
			img.offy = -z + persistentoffy;
			IsoManager.add(img);
			IsoManager.add(simg);			
		#end
		
		switch(style)
		{
			case(0):
			{
				shotup = up_player_straight;
				hit = player_hit;
			}
			case(1):
			{
				shotup = up_player_volley;
				hit = player_hit;
			}
			case(2):
			{
				shotup = up_player_straight;
				hit = player_hithard;
			}
		}
		
		Game.cur.shooterman.projectiles.push(this);
		
	}

	public function player_hit()
	{

		parent.shots_hit += Creep.hurtpoint(desttx,destty,1);
		alive = false;
		#if flash9
		IsoManager.remove(img);
		IsoManager.remove(simg);
		img = null;		
		simg = null;		
		#end
	}

	public function player_hithard()
	{
		Creep.hurtpoint(desttx,destty,1);
		player_hit();
	}
	
	public function up_player_straight()
	{
		// linear path to ground at target
		var cvecx : Float = (x-destx);
		var cvecy : Float = (y-desty);		
		var curdist : Float = Math.sqrt((cvecx*cvecx)+(cvecy*cvecy));
		
		if (curdist>speed)
		{
			x -= speed*(vecx/startdist);
			y -= speed*(vecy/startdist);
			z = curdist/startdist * startz;
			#if flash9
				img.px = x;
				img.py = y;
				img.offy = -z + persistentoffy;
				var shadowt : IntPoint = IsoMetrics.pixtotile_xy(x,y);
				simg.visible = true;
				var ct : GameTile = Game.cur.map[shadowt.x][shadowt.y];
				if (!(ct.terrain>=0))
				{
					simg.offy = persistentoffy +15;
				}
				else if (ct.fort!=null && z<18)
				{
					simg.visible = false; // it looks really bad 
										  // if we have shadows on top and the bullet going through a wall
				}
				else
				{
					simg.offy = persistentoffy;
				}
				simg.px = x;
				simg.py = y;
				img.moveup();
				simg.moveup();
			#end
		}
		else
		{
			x = destx;
			y = desty;
			hit();
		}		
		
	}

	public function up_player_volley()
	{
		// arc from top of turret to ground
		var cvecx : Float = (x-destx);
		var cvecy : Float = (y-desty);		
		var curdist : Float = Math.sqrt((cvecx*cvecx)+(cvecy*cvecy));
		
		if (curdist>speed)
		{
			x -= speed*(vecx/startdist);
			y -= speed*(vecy/startdist);
			z = Math.sin((curdist/startdist)*3.141) * startdist/2;
			#if flash9
				img.px = x;
				img.py = y;
				img.offy = -z + persistentoffy;
				var shadowt : IntPoint = IsoMetrics.pixtotile_xy(x,y);
				simg.visible = true;
				if (!(Game.cur.map[Std.int(shadowt.x)][Std.int(shadowt.y)].terrain>=0))
				{
					simg.offy = persistentoffy +15;
				}
				else if (Game.cur.map[shadowt.x][shadowt.y].fort!=null && z<18)
				{
					simg.visible = false; // it looks really bad 
										  // if we have shadows on top and the bullet going through the wall
				}
				else
				{
					simg.offy = persistentoffy;
				}
				simg.px = x;
				simg.py = y;
				img.moveup();
				simg.moveup();
			#end
		}
		else
		{
			x = destx;
			y = desty;
			hit();
		}		
		
	}

	static public function start()
	{
	
	}

	static public function end()
	{
	
	}
	
	static public function up()
	{
	
		var newproj : Array<Projectile> = new Array();
		
		/*for (p in 0...projectiles.length)
		{
			var cp : Projectile = projectiles.pop();
			if (cp.alive==true)
			{
				cp.shotup();			
				newproj.push(cp);
			}
		}
		
		projectiles = newproj;*/
		
		
		for (p in Game.cur.shooterman.projectiles)
		{
			p.shotup();
			if (p.alive==false)
			{
				Game.cur.shooterman.projectiles.remove(p);
			}
		}
		
	}
	
	
}
